import json
import time

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.db.models import Q
from user import models
from utils.jwtool import tool

# Create your views here.
class LoginView(APIView):
    def post(self,request):
        account=request.data.get('account')
        password=request.data.get('password')
        query=Q(username=account) | Q(email=account) | Q(phone=account)
        info=models.UserModel.objects.filter(query).first()
        if not  info:
            return Response({'msg':'账号不存在','code':400})

        if info.password==password:
            token=tool.get_token(info.id)
            return Response({'msg':'登陆成功','code':200,'data':{'token':token,'userid':info.id}})

        else:
            return Response({'msg':'密码错误','code':400})


import re
import random
from utils.mysdk import send_message
from utils.myredis import redistool

from .task import sendmsg

class SendCode(APIView):
    def get(self,request):
        phone=request.query_params.get("phone")
        #写一个正则验证手机号的合法性
        result=re.compile(r"^1[3-9]\d{9}$")
        if not result.match(phone):#如果手机号不合法
            return Response({"msg":"手机号不合法","code":400})
        #手机号合法 发送验证码
        code=str(random.randint(1000,9999))
        #调用发送验证码的函数
        #res=send_message(phone,code)
        #调用异步函数
        task=sendmsg.delay(phone,code)#异步任务的调用
        #拿到异步任务的结果
        res=task.get()
        #将res为json格式
        resjson=json.loads(res)
        if resjson["statusCode"]=="000000":
            redistool.setcode(phone,code)
            #发送成功后将code存储到redis中
            return Response({"msg":"发送成功","code":200})
        else:
            return Response({"msg":"发送失败","code":400})


class LoginCode(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        code=request.data.get('code')
        #写一个正则验证几哦啊杨合法
        result=re.compile(r'^1[3-9]\d{9}$')
        if not result.match(phone):
            return Response({'msg':'手机号不合法','code':400})

        rediscode=redistool.getcode(phone)
        if code!=rediscode:
            return Response({'msg':'验证码错误','code':400})
        user=models.UserModel.objects.filter(phone=phone).first()

        if not user:
            user=models.UserModel.objects.create(phone=phone)

        token=tool.get_token(user.id)
        return Response({'msg': '登陆成功', 'code': 200, 'data': {'token': token, 'userid': user.id}})

class Register(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        password=request.data.get('password')
        againpassword=request.data.get('againpassword')
        code=request.data.get('code')
        result=re.compile(r'^1[3-9]\d{9}$')
        if not result.match(phone):
            return Response({'msg':'手机号不合法','code':400})
        if password !=againpassword:
            return Response({'code':400,'msg':'密码不一致'})
        if  code!=redistool.getcode(phone):
            return Response({'code':400,'msg':'验证码错误'})

        user = models.UserModel.objects.filter(phone=phone).first()
        if not user:
            user = models.UserModel.objects.create(phone=phone)
            user.save()
            # token = tool.get_token(user.id)

            return Response({'msg': '注册成功', 'code': 200})



        token = tool.get_token(user.id)
        return Response({'msg': '登陆成功', 'code': 200, 'data': {'token': token}})




from .task import send_sms
class TestView(APIView):
    def get(self,request):
        #调用任务
        send_sms.delay("小三","下大雨")
        return Response({"msg":"测试成功","code":200})